#include <graphics.h>
#include <conio.h>

#pragma comment(lib,"winmm.lib")
#define High 864
#define Width 591

IMAGE img_bk;
IMAGE img_planeNormal1,img_planeNormal2,img_bullet1,img_bullet2,img_enemyplane1,img_enemyplane2;
int position_x,position_y;
int bullet_x,bullet_y;
float enemy_x,enemy_y;

void startup()
{
	mciSendString("open C:\\game_music.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);

	initgraph(Width,High);
	loadimage(&img_bk,"C:\\background.jpg");
	loadimage(&img_planeNormal1,"C:\\planeNormal_1.jpg");
	loadimage(&img_planeNormal2,"C:\\planeNormal_2.jpg");
	loadimage(&img_bullet1,"C:\\bullet1.jpg");
	loadimage(&img_bullet2,"C:\\bullet2.jpg");
	loadimage(&img_enemyplane1,"C:\\enemyPlane1.jpg");
	loadimage(&img_enemyplane2,"C:\\enemyPlane2.jpg");
	position_x=Width*0.5;
	position_y=High*0.7;
	enemy_x=Width*0.5;
	enemy_y=0;

	BeginBatchDraw();	
}
void show()
{
	putimage(0,0,&img_bk);	
	putimage(position_x-50,position_y-60,&img_planeNormal1,NOTSRCERASE);
	putimage(position_x-50,position_y-60,&img_planeNormal2,SRCINVERT);
	putimage(bullet_x,bullet_y,&img_bullet1,NOTSRCERASE);
	putimage(bullet_x,bullet_y,&img_bullet2,SRCINVERT);
	putimage(enemy_x,enemy_y,&img_enemyplane1,NOTSRCERASE);
	putimage(enemy_x,enemy_y,&img_enemyplane2,SRCINVERT);


	FlushBatchDraw();
	Sleep(5);
}
void updateWithoutInput()
{
	if(bullet_y>-50)
	{
		bullet_y=bullet_y-2;
	}
	if(enemy_y<High)
		enemy_y=enemy_y+0.5;
	else
	{
		enemy_x=rand()%Width;
		enemy_y=0;
	}
	if(abs(enemy_x-bullet_x)+abs(enemy_y-bullet_y)<50)
	{
		enemy_x=rand()%Width;
		enemy_y=0;
	}
	if(abs(enemy_x-position_x)+abs(enemy_y-position_y)<150)
	{
		exit(0);
	}

}
void updateWithInput()
{
	MOUSEMSG m;
	while(MouseHit())
	{
		m=GetMouseMsg();
		if(m.uMsg==WM_MOUSEMOVE)
		{
			position_x=m.x;
			position_y=m.y;

		}
		else if(m.uMsg==WM_LBUTTONDOWN)
		{
			bullet_x=position_x-5;
			bullet_y=position_y-85;
			mciSendString("close f_gun",NULL,0,NULL);
			mciSendString("open C:\\f_gun.mp3 alias f_gun",NULL,0,NULL);
			mciSendString("play f_gun",NULL,0,NULL);
		}
	}
}


void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	gameover();	
	return 0;
}